package com.namecard.namecardapi.controller;


import cn.hutool.core.io.FileUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.namecard.namecardapi.entity.TImg;
import com.namecard.namecardapi.entity.vo.TImgQueryVo;
import com.namecard.namecardapi.result.Result;
import com.namecard.namecardapi.service.TImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xb
 * @since 2024-04-21
 */
@DS("slave2")
@Api(tags = "图片管理") //Swagger生成api的提示
@RestController
@RequestMapping("/admin/system/t-img")
public class TImgController {

    @Value("${files.upload.path}")
    private String path;//磁盘文件路径

    @Resource
    private TImgService imgService;

    //文件上传
    @ApiOperation(value = "文件上传")
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        String url = imgService.uploadDoc(file,path);
        return Result.ok(url);
    }

    //文件下载
    @ApiOperation(value = "文件下载")
    @GetMapping("/download")
    public void download(String fileUid, HttpServletResponse response) throws IOException {
        // 根据文件的唯一标识码获取文件
        File file = new File(path + fileUid);
        // 设置输出流的格式
        ServletOutputStream os = response.getOutputStream();
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileUid, "UTF-8"));
        response.setContentType("application/octet-stream");
        // 读取文件的字节流
        os.write(FileUtil.readBytes(file));
        os.flush();
        os.close();
    }

    //分页查询
    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{limit}")
    public Result page(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "tImgQueryVo", value = "查询对象", required = false)
            TImgQueryVo tImgQueryVo) {
        Page<TImg> pageParam = new Page<>(page, limit);
        IPage<TImg> pageModel = imgService.selectPage(pageParam, tImgQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/batch")
    public Result batch(@RequestBody List<Long> idList) {
        imgService.removeByIds(idList);
        return Result.ok();
    }
}

